<?php

/**
 * LEAGUEMan
 *
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');

$currentMatch	= LM::getMatch();
$match = null;

if ($currentMatch > 0) {
    $match = LM::getLMTable("Match");
    $match->load($currentMatch);
}

if (!$match instanceof LM_Table_Match) {
    LM::redirect("matches");
}

switch($task) {

    case 'quickadd':

        $eventMatch     = intval($match->match_id);
        $eventMinute    = intval(mosGetParam($_POST, "event_minute", 0));
        $eventPlayermap	= intval(mosGetParam($_POST, "event_playermap", 0));
        $eventType      = intval(mosGetParam($_POST, "event_type", 0));

        if ($eventPlayermap != 0) {
            $row = LM::getLMTable("Event");
            $row->event_match		= $eventMatch;
            $row->event_minute		= $eventMinute;
            $row->event_playermap	= $eventPlayermap;
            $row->event_type		= $eventType;

            if ($row->store()) {

                $eventtype = LM_Eventtypemap::getEventtype($eventType);
                if ($eventtype instanceof LM_Table_Eventtype) {
                    $association = $eventtype->getAssociation();

                    $sql = "UPDATE #__leagueman_playermap
                               SET $association = $association + 1
                             WHERE playermap_id = " . $database->Quote(intval($eventPlayermap)) . " LIMIT 1";
                    $database->setQuery($sql);
                    $database->query();

                    if ($association == "cnt_goals" || $association == "cnt_own_goals") {
                        $tempresult = new MatchEventTempResult($match);
                        $row->event_text_short = $tempresult->getTempResult1() . ":" . $tempresult->getTempResult2();
                        $row->store();
                    }
                }
            }
        }

        LM::redirect("events");

        break;

    case 'edit':

        $row = LM::getLMTable("Event");

        if ($task == "edit") {
            $row->load($cid[0]);
        }

        $team1 = $match->getTeam1();
        $team2 = $match->getTeam2();

        $playertable = LM::getLMTable("Playermap");
        $playertable->filterbyProject($project->getID());
        $playertable->filterbyTeam($team1->getID());
        $team1_players = $playertable->getRows("", 0,0);

        $playertable = LM::getLMTable("Playermap");
        $playertable->filterbyProject($project->getID());
        $playertable->filterbyTeam($team2->getID());
        $team2_players = $playertable->getRows("", 0,0);

        $playerlist[] = mosHTML::makeOption(0, "Select Player");
        $playerlist[] = mosHTML::makeOption(-1, "<b>" . $team1->getName() . "</b>");
        foreach ($team1_players as $player) {
            $playerlist[] = mosHTML::makeOption($player->playermap_id, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $player->player_name);
        }
        $playerlist[] = mosHTML::makeOption(-1, $team2->team_name);
        foreach ($team2_players as $player) {
            $playerlist[] = mosHTML::makeOption($player->playermap_id, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $player->player_name);
        }

        $eventtypes[] = mosHTML::makeOption(0, "Select Eventtype");

        $eventtypemap = LM_Eventtypemap::getInstance();
        $projecteventlist = $project->getEventtypes();

        foreach ($eventtypemap->getEventtypes() AS $eventtype) {
            if (in_array($eventtype->getID(), $projecteventlist)) {
                $eventtypes[] = mosHTML::makeOption($eventtype->getID(), $eventtype->getName());
            }
        }

        $template = LM::getTemplate();

        $template->assign("view", "admin/form_events.tpl.php");
        $template->assign("project", $project);
        $template->assign("team", $team);
        $template->assign("match", $match);
        $template->assign("players", $playerlist);
        $template->assign("eventtypes", $eventtypes);
        $template->assign("my", $my);
        $template->assign("row", $row);
        $template->display("admin/view.tpl.php");
        break;

    case 'save':
        $matchevent = LM::getLMTable("Event");
        $matchevent->save($_POST);
        LM::redirect("events");
        break;

    case 'cancel':
        LM::redirect("events");
        break;

    case 'remove':
        $row = LM::getLMTable("Event");
        foreach ($cid AS $c) {
            $row->delete($c);
        }
        LM::redirect("events");
        break;

    default:

        $team1 = $match->getTeam1();
        $team2 = $match->getTeam2();

        $playermap = LM::getLMTable("Playermap");
        $playermap->filterbyProject($project->getID());
        $playermap->filterbyTeam($team1->team_id);
        $team1_players = $playermap->getRows("", 0,0);

        $playermap = LM::getLMTable("Playermap");
        $playermap->filterbyProject($project->getID());
        $playermap->filterbyTeam($team2->team_id);
        $team2_players = $playermap->getRows("", 0,0);

        $playerlist[] = mosHTML::makeOption(0, "Select Player");
        $playerlist[] = mosHTML::makeOption(-1, "<b>" . $team1->team_name . "</b>");
        foreach ($team1_players as $player) {
            $playerlist[] = mosHTML::makeOption($player->playermap_id, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $player->player_name);
        }
        $playerlist[] = mosHTML::makeOption(-1, $team2->team_name);
        foreach ($team2_players as $player) {
            $playerlist[] = mosHTML::makeOption($player->playermap_id, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $player->player_name);
        }

        $eventtypes[] = mosHTML::makeOption(0, "Select Eventtype");

        $eventtypemap = LM_Eventtypemap::getInstance();
        $projecteventlist = $project->getEventtypes();

        foreach ($eventtypemap->getEventtypes() AS $eventtype) {
            if (in_array($eventtype->getID(), $projecteventlist)) {
                $eventtypes[] = mosHTML::makeOption($eventtype->getID(), $eventtype->getName());
            }
        }

        $query = "SELECT e.*, pm.playermap_id, p.player_name, t.team_id
                    FROM #__leagueman_event e
              INNER JOIN #__leagueman_playermap pm ON pm.playermap_id = e.event_playermap
              INNER JOIN #__leagueman_player p ON pm.playermap_player = p.player_id
              INNER JOIN #__leagueman_team t ON pm.playermap_team = t.team_id
                   WHERE e.event_match = {$match->match_id}
                ORDER BY e.event_minute";
        $database->setQuery($query);
        $rows = $database->loadObjectList();

        $template = LM::getTemplate();

        $template->assign("view", "admin/list_events.tpl.php");
        $template->assign("project", $project);
        $template->assign("team", $team);
        $template->assign("match", $match);
        $template->assign("players", $playerlist);
        $template->assign("eventtypes", $eventtypes);
        $template->assign("rows", $rows);
        $template->assign("my", $my);
        $template->display("admin/view.tpl.php");
        break;
}